<template>
  <el-scrollbar :wrap-class="{wrapClass: true}">
    <div class="app-container">
      <div class="filter-container">
        <el-button
          size="small"
          class="filter-item right"
          icon="el-icon-refresh"
          type="primary"
          plain
          @click="refresh"
        >
          刷新
        </el-button>
        <div style="clear: both"></div>
      </div>
      <el-table
        ref="multipleTable"
        v-loading="listLoading"
        :data="list"
        :height="listQuery.total>0 ? 'calc(100vh - 205px)' : 'calc(100vh - 170px)'"
        :header-cell-style="{background: '#F2F6FC'}"
        element-loading-text="Loading"
        fit
        border
        size="medium"
      >
        <el-table-column align="center" label="序号" width="50" type="index" />
        <el-table-column label="名称" min-width="200px" prop="name">
        </el-table-column>
        <el-table-column label="状态" width="80" align="center">
          <template slot-scope="{row}">
            <strong class="">{{ row.stateText }}</strong>
          </template>
        </el-table-column>
        <el-table-column label="点击量" align="center" prop="clickCount">
        </el-table-column>
        <el-table-column label="回复数" align="center" prop="replyCount">
        </el-table-column>
        <el-table-column label="转发数" align="center" prop="shareCount">
        </el-table-column>
        <el-table-column label="点赞数" align="center" prop="likeCount">
        </el-table-column>
        <el-table-column label="创建时间" width="160px" align="center" prop="createdAt">
        </el-table-column>
        <el-table-column align="center" fixed="right" label="操作" width="200">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              plain
              icon="el-icon-edit"
              @click="restoreRow(scope.row.id)"
            >恢复
            </el-button>

            <el-popconfirm
              title="确定删除吗？"
              @confirm="deleteRow(scope.row.id)"
              style="margin-left: 10px"
            >
              <el-button
                type="danger"
                size="mini"
                plain
                icon="el-icon-delete"
                slot="reference"
              >删除
              </el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <pagination
        v-show="listQuery.total>0"
        :total="listQuery.total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="fetchData"
      />
    </div>
  </el-scrollbar>
</template>

<script lang="ts">
import Pagination from '@/components/Pagination/index.vue'
import { Component, Vue } from 'vue-property-decorator'
import { getAction, postAction, deleteAction } from '@/api/common'

@Component({
  name: 'index',
  components: {
    Pagination
  }
})
export default class extends Vue {
  private list: any[] = []
  private listLoading = false
  private listQuery = {
    page: 1,
    total: 0,
    limit: 20
  }

  private selectId = 0

  created() {
    this.fetchData()
  }

  private async fetchData() {
    this.listLoading = true
    const { data } = await getAction(this.listQuery, 'socialMarketingRecycleBin')
    this.list = data.items
    this.listQuery.total = data.total
    this.listLoading = false
  }

  private search() {
    this.listQuery.page = 1
    this.fetchData()
  }

  private async restoreRow(id: number) {
    const res: any = await postAction(null, `socialMarketingRecycleBin/${id}`)
    if (res.code === 20000) {
      this.$message.success('恢复成功')
    } else {
      this.$message.error(res.message)
    }
    this.refresh()
  }

  private async deleteRow(id: number) {
    const res: any = await deleteAction(null, `socialMarketingRecycleBin/${id}`)
    if (res.code === 20000) {
      this.$message.success('删除成功')
    } else {
      this.$message.error(res.message)
    }
    this.refresh()
  }

  private refresh() {
    this.fetchData()
  }
}
</script>

<style scoped>

</style>
